package com.yishusmt.wechatapi.wechatdev.adivce;

import com.yishusmt.wechatapi.wechatdev.dto.base.Result;
import com.yishusmt.wechatapi.wechatdev.enums.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.util.Set;

/**
 * 统一异常捕获
 *
 * @author zhangyi
 * @date 2018/9/11 9:53
 */
//@ControllerAdvice
//@Component
//@Slf4j
public class GlobalExceptionHandler extends RuntimeException{
//
//    @ExceptionHandler
//    @ResponseBody
//    @ResponseStatus(HttpStatus.BAD_REQUEST)
//    public Result handler(ValidationException e){
//        StringBuffer stringBuffer = new StringBuffer();
//        if(e instanceof ConstraintViolationException){
//            ConstraintViolationException ex = (ConstraintViolationException)e;
//            Set<ConstraintViolation<?>> constraintViolations = ex.getConstraintViolations();
//            for(ConstraintViolation<?> c : constraintViolations){
//                stringBuffer.append(c.getMessage());
//                log.error(c.getMessage());
//                System.out.println(c.getMessage());
//            }
//        }
//        return new Result(ResultCodeEnum.APPEND_DATA_ERROR,"全局捕获异常错误..."+stringBuffer.toString());
//    }
}
